/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 9/9/2010
*/

@isTest
private class AccountTeamControllerTest {
	private static ID curAccId;
	private static ID curOppId;
	private static list<ID> memberUserIds;
	private static User admin_user;
	private static UserTeamMember utm;
	
	private static void init(){
		User u_admin = new User();
		u_admin = new User();
		u_admin.UserName = 'admin_xyz_1234567890@atti-temp.com';
		u_admin.LastName = 'admin Test Email';
		u_admin.Email = 'admin_xyz_1234567890@atti.com';
		u_admin.Alias = 'adminxxx';
		u_admin.CommunityNickname = 'admin_xyaz_1234556';
		u_admin.TimeZoneSidKey = 'America/Los_Angeles';
		u_admin.LocaleSidKey = 'en_US';
		u_admin.EmailEncodingKey = 'UTF-8';
		u_admin.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		u_admin.UserRoleId = [select ID from UserRole where Name like 'Information Systems'limit 1].Id;
		u_admin.LanguageLocaleKey = 'en_US';
		u_admin.IsActive = true;
		Database.Saveresult s_sr_admin = Database.insert(u_admin);
		admin_user = [select id from User where id =: s_sr_admin.getId() limit 1];
		
		list<User> users = new list<User>();
		for(Integer i=0; i < 4 ; i++){
			User u_new = new User();
			u_new.UserName = 'xyz_1234567890@atti-temp.com' + i;
			u_new.LastName = 'Michael Shu Test Email' + i;
			u_new.Email = 'xyz_1234567890@atti.com';
			u_new.Alias = 'abcde' + i;
			u_new.CommunityNickname = 'xyaz_1234556' + i;
			u_new.TimeZoneSidKey = 'America/Los_Angeles';
			u_new.LocaleSidKey = 'en_US';
			u_new.EmailEncodingKey = 'UTF-8';
			u_new.ProfileId = [select ID from Profile where Name like '%National Sales%' limit 1].Id;
			u_new.UserRoleId = [select ID from UserRole where Name like '%National Sales%'limit 1].Id;
			u_new.LanguageLocaleKey = 'en_US';
			u_new.IsActive = true;
			users.add(u_new);
		}
		memberUserIds = new list<Id>();
		System.runAs(admin_user){
			Database.Saveresult[] s_sr = Database.insert(users);
			for(Database.Saveresult r : s_sr){
				memberUserIds.add(r.getId());
			}
		}
		
		UserAccountTeamMember[] uat = [select TeamMemberRole, UserId, OwnerId from UserAccountTeamMember limit 1];
		UserTeamMember[] utms = [select TeamMemberRole, UserId, OwnerId from UserTeamMember limit 1];
		if(!utms.isEmpty()){
			utm = utms[0];
		}
		
		Account acct = new Account();
		acct.Name = 'Test Account Team';
		if(!uat.isEmpty()){
			acct.OwnerId = uat[0].OwnerId;
		}else{
			acct.OwnerId = memberUserIds[0];
		}
		acct.RecordTypeId = RecordTypeHelper.GetRecordTypeId('Account', 'CMR Agency Client');
		System.runAs(admin_user){
			Database.Saveresult sr1 = Database.insert(acct); 
			curAccId = sr1.getId();
		}
		
		AccountTeamMember new_ateam = new AccountTeamMember();
		new_ateam.AccountId = curAccId;
		new_ateam.TeamMemberRole = 'GM';
		new_ateam.UserId = memberUserIds[1];
		System.runAs(admin_user){
			Database.Saveresult at_sr = Database.insert(new_ateam);
		}				
		
		Opportunity opp = new Opportunity();
		opp.Name = 'Test Opportunity Team';
		opp.StageName = 'FirstStage';
		opp.CloseDate = Date.today();
		opp.AccountId = curAccId;
		if(!uat.isEmpty()){
			opp.OwnerId = uat[0].OwnerId;
		}else{
			opp.OwnerId = memberUserIds[0];
		}
		opp.RecordTypeId = RecordTypeHelper.GetRecordTypeId('Opportunity', 'Contract');
		System.runAs(admin_user){
			Database.Saveresult sr2 = Database.insert(opp);
			curOppId = sr2.getId();
		}
		
		OpportunityTeamMember otm = new OpportunityTeamMember();
		otm.OpportunityId = curOppId;
		otm.UserId = memberUserIds[2];
		otm.TeamMemberRole = 'EST';
		System.runAs(admin_user){
			Database.Saveresult otm_sr = Database.insert(otm);
		}
	}
	
	public static testMethod void testAddAccountTeam() {
		init();
		//PageReference pageRef = Page.AccountTeamPage;
		//Test.setCurrentPage(pageRef);
		AccountTeamController controller = new AccountTeamController();
		controller.accId = curAccId;
		System.assertEquals(1, controller.accTeams.size());
		controller.oppId = curOppId;
		System.assertEquals(true, controller.hasAccount);
		System.assertEquals(2, controller.oppTeams.size());
		controller.displayAccess();
		controller.deleteAccTeam();
		controller.deleteAllAccTeam();
		controller.addDefaultAccTeams();
		System.runAs(admin_user){
			System.assertEquals(true, controller.hasDMLpermssion);
		}
		controller.addDefaultOppTeams();
	}
	
	public static testMethod void testAddOppTeam() {
		init();
		if(utm != null){
			Opportunity o = [select id from Opportunity where Id =: curOppId];
			o.OwnerId = utm.OwnerId;
			System.runAs(admin_user){
				//update o;
			}
		}
		AccountTeamController controller = new AccountTeamController();
		controller.oppId = curOppId;
		controller.displayAccess();
		controller.deleteOppTeam();
		controller.deleteAllOppTeam();
		controller.addDefaultOppTeams();
		controller.addOppTeamsFromParentAccTeams();
	}	

}